package com.cqeec.bootdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.bootdemo.Service.GradeService;
import com.cqeec.bootdemo.entity.Employee;
import com.cqeec.bootdemo.entity.Grade;
import com.cqeec.bootdemo.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/grade")
public class GradeController {
    @Autowired
    private GradeService gradeService;
    @PostMapping("/getGradeByName")
    public R<List<Grade>> getGradeByName(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(Grade::getName,"ao").orderByDesc(Grade::getVue);
        List<Grade> list=gradeService.list(wrapper);
        //如果成功，调用success方法，返回数据
        //如果失败,调用error方法，返回错误信息
        return R.success(list);
    }
    @PostMapping("/getGrade")
    public R<List<Grade>> getGrade(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.gt(Grade::getJava,75).gt(Grade::getVue,75).orderByDesc(Grade::getPython);
        List<Grade> list=gradeService.list(wrapper);
        return R.success(list);
    }
    @PostMapping("/getGradeByJava")
    public R<List<Grade>> getGradeByJava(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Grade::getJava,80).or().lt(Grade::getPython,80).orderByDesc(Grade::getId);
        List<Grade> list=gradeService.list(wrapper);
        return R.success(list);
    }
    @PostMapping("/getList")
    public List<Grade> getAll(){
        return gradeService.list();
    }
    @GetMapping("/getListByVue")
    public List<Grade> getListByVue(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Grade::getVue).
                like(Grade::getName,"ao");
        return gradeService.list(wrapper);
    }
    @GetMapping("/getListByPython")
    public List<Grade> getListByPython(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Grade::getPython).
               or().gt(Grade::getJava,"75").or().gt(Grade::getVue,"75");
        return gradeService.list(wrapper);

    }
    @GetMapping("/GetListByid")
    public List<Grade> getListByid(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Grade::getId).lt(Grade::getJava,"80")
                .lt(Grade::getPython,"80");
        return gradeService.list(wrapper);
    }
    @PostMapping("/addGrade")
    public R<String> add(){
        Grade grade=new Grade();
        grade.setName("xiaoli");
        grade.setJava(76);
        grade.setPython(97);
        grade.setVue(68);
       boolean save= gradeService.save(grade);
        if (save){
            return R.success("新增成功");
        }else {
            return R.error("新增失败");
        }
    }
    @PutMapping("/update")
    public R<String> update(){
        //update grade set java=90 vue=100 name=xiaozi where id=3;
        //修改条件
        LambdaUpdateWrapper<Grade> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(Grade::getId,3).set(Grade::getJava,90)
                .set(Grade::getVue,100).set(Grade::getName,"xiaozi");
     boolean update= gradeService.update(wrapper);
     if (update){
         return R.success("修改成功");
     }
        return R.error("修改失败");
    }
    //updateByid
    //删除,批量删除
    //批量新增
    @PostMapping("/addOrUpdate")
    public R<String> addOrUpdate(){
        Grade grade=new Grade();
        grade.setId(10);
        grade.setName("xiaohei");
        grade.setJava(50);
        grade.setPython(50);
        grade.setVue(65);
        boolean b=gradeService.saveOrUpdate(grade);
        if (b){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    // 删除单个
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean result = gradeService.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }
    @PostMapping("/getPage/{current}/{size}")
    public R<Page> getPage(@PathVariable int current,@PathVariable int size){
        //创建一个page对象
        Page<Grade> page=new Page<>(current,size);
        //条件查询
        LambdaUpdateWrapper<Grade> wrapper=new LambdaUpdateWrapper<>();
        wrapper.gt(Grade::getJava,60).orderByDesc(Grade::getId);
        Page<Grade> page1= gradeService.page(page,wrapper);
        return R.success(page1);
    }
}
